﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Data.Car_Rental;
using Entity.Car_Rental;

namespace Background.Controllers
{
    public class ProductPropertyTypeController : Common.Background.Web.Controllers.BackgroundBaseController
    {
        //
        // GET: /ProductPropertyType/

        IProductPropertyTypeRepository iProductPropertyTypeRepository = null;
        public ProductPropertyTypeController()
        {
            iProductPropertyTypeRepository = new ProductPropertyTypeRepository();
        }
        public ActionResult Index()
        {
            var model = iProductPropertyTypeRepository.GetModel();
            return View(model);
        }


        public ActionResult Details(int? id)
        {
            if (id.HasValue)
                return View(iProductPropertyTypeRepository.Find(id.Value));
            return View();
        }
        [HttpPost]
        public ActionResult Details(FormCollection form)
        {
            ProductPropertyType entity = new ProductPropertyType();
            TryUpdateModel(entity);
            if (entity.IsValid)
            {
                if (entity.ID == 0)
                    iProductPropertyTypeRepository.Insert(entity);
                else
                    iProductPropertyTypeRepository.Update(entity);
                ProductPropertyTypeRepository.ReLoad(); //重新装载
                return AlertToUrl("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
            }
            return View(iProductPropertyTypeRepository.Find(entity.ID));
        }
        public ActionResult Delete(int id)
        {
            iProductPropertyTypeRepository.Delete(new ProductPropertyType { ID = id });
            return AlertToUrl("Index");
        }
    }
}
